package com.meteor.downloader.videodownloader.activity.downloader

import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.activity.MdmBindingActivity
import com.meteor.downloader.videodownloader.adapter.AdapterLibraryItem
import com.meteor.downloader.videodownloader.admdm.MdmDealAdUtils
import com.meteor.downloader.videodownloader.databinding.ActivityMdmVideoManagerBinding
import com.meteor.downloader.videodownloader.dismissGoMdm
import com.meteor.downloader.videodownloader.mdmdata.DataMdmFile
import com.meteor.downloader.videodownloader.showToastIDMdm
import com.meteor.downloader.videodownloader.utils.MdmTotalUtils
import com.meteor.downloader.videodownloader.utils.dialog.DialogCanNextMdm
import com.meteor.downloader.videodownloader.utils.dialog.DialogInitMdmLoading
import com.meteor.downloader.videodownloader.utils.dialog.DialogRenameFileMdm
import com.meteor.downloader.videodownloader.utils.value.ParamsMdmUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MdmVideoManagerActivity : MdmBindingActivity<ActivityMdmVideoManagerBinding>() {
    private var mDialogInitMdmLoading: DialogInitMdmLoading? = null
    private lateinit var mAdapterLibraryItem: AdapterLibraryItem
    private val mVideoList = mutableListOf<DataMdmFile>()
    override fun doBackPressed() {
        showBookAdMdm("mdmBack") {
            super.doBackPressed()
        }
    }

    override fun beginViewAndDoMdm() {
        mStarNativeValue = true
        mNativeBookLayout = mMdmBinding.flMdmNativeView
        mMdmBinding.topSetMdm.tvTitleAll.text = getString(R.string.mdm_video_library)
        mDialogInitMdmLoading =
            DialogInitMdmLoading(this@MdmVideoManagerActivity, R.string.mdm_loading)
        mDialogInitMdmLoading?.show()
        initRecycler()
        getFileData()
        lifecycleScope.launch(Dispatchers.Main) {
            delay(288)
            MdmDealAdUtils.loadBookIntersMdm()
        }
    }

    private fun initRecycler() {
        mAdapterLibraryItem = AdapterLibraryItem(this, mVideoList, {
            showBookAdMdm("", true) {
                val data = mVideoList[it]
                MdmTotalUtils.playVideoMdm(this, data.mFileMdm)
            }
        }, {
            //rename
            showBookAdMdm("", true) {
                val data = mVideoList[it]
                val dialog = DialogRenameFileMdm(this, data.mFileMdm) { newFile ->
                    if (data.mFileMdm.renameTo(newFile)) {
                        data.mFileMdm = newFile
                        mAdapterLibraryItem.notifyItemChanged(it)
                    } else {
                        showToastIDMdm(R.string.mdm_failed_modify_filename)
                    }
                }
                dialog.show()
            }
        }, {
            //share
            showBookAdMdm("", true) {
                val data = mVideoList[it]
                MdmTotalUtils.shareVideoMdm(this, data.mFileMdm)
            }
        }) {
            //delete
            showBookAdMdm("", true) {
                DialogCanNextMdm(this, null, R.string.mdm_please_confirm_delete) {
                    val data = mVideoList[it]
                    if (data.mFileMdm.delete()) {
                        mVideoList.removeAt(it)
                        mAdapterLibraryItem.notifyItemRemoved(it)
                        if (mVideoList.isEmpty()) {
                            mMdmBinding.clNoDataList.visibility = View.VISIBLE
                            mMdmBinding.rvMdmList.visibility = View.GONE
                        }
                    }
                }.show()
            }
        }
        mMdmBinding.rvMdmList.layoutManager = GridLayoutManager(this, 2)
        mMdmBinding.rvMdmList.adapter = mAdapterLibraryItem
    }

    private fun getFileData() {
        mVideoList.clear()
        lifecycleScope.launch(Dispatchers.IO) {
            delay(400)
            launch(Dispatchers.IO) {
                ParamsMdmUtils.mSaveFileMdm.walk().filter {
                    it.length() > 0L && it.isFile
                }.forEach {
                    mVideoList.add(DataMdmFile(it))
                }
                mVideoList.sortWith { f1, f2 ->
                    f2.mFileMdm.lastModified().compareTo(f1.mFileMdm.lastModified())
                }
            }
        }.invokeOnCompletion {
            showData()
        }
    }

    private fun showData() {
        lifecycleScope.launch {
            if (mVideoList.isEmpty()) {
                mMdmBinding.clNoDataList.visibility = View.VISIBLE
                mMdmBinding.rvMdmList.visibility = View.GONE
            } else {
                mMdmBinding.rvMdmList.visibility = View.VISIBLE
                mMdmBinding.clNoDataList.visibility = View.GONE
                mAdapterLibraryItem.notifyDataSetChanged()
            }
            delay(120)
            mDialogInitMdmLoading?.dismissGoMdm()
        }
    }

    private fun showBookAdMdm(
        type: String, showTip: Boolean = false, statusLook: Boolean = true, mdmBack: () -> Unit
    ) {
        if (statusLook && !MdmTotalUtils.getMdmAppStatus()) {
            mdmBack()
            return
        }
        if (showTip) {
            if (MdmDealAdUtils.getVlaueForInters() != null) {
                showRootBookAdBack(type, statusLook, mdmBack)
                return
            }
            val dialog = DialogInitMdmLoading(this)
            dialog.show()
            MdmDealAdUtils.loadBookIntersMdm(true, statusLook)
            lifecycleScope.launch(Dispatchers.Main) {
                delay(1600)
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(96)
                    dialog.dismissGoMdm()
                    return@launch
                }
                if (MdmDealAdUtils.getVlaueForInters() != null) {
                    delay(1600)
                    showRootBookAdBack(type, statusLook, mdmBack)
                    delay(100)
                    dialog.dismissGoMdm()
                    return@launch
                }
                delay(1600)
                showRootBookAdBack(type, statusLook, mdmBack)
                delay(96)
                dialog.dismissGoMdm()
            }
        } else {
            showRootBookAdBack(type, statusLook, mdmBack)
        }
    }

    override fun getLayoutValue(): ActivityMdmVideoManagerBinding {
        return ActivityMdmVideoManagerBinding.inflate(layoutInflater)
    }
}